package com.shinlooker.wzyd.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shinlooker.wzyd.adapter.ImageResourceAdapter;
import com.shinlooker.wzyd.R;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.indicator.enums.IndicatorStyle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private BannerViewPager mViewPager;
    private List<Integer> imagList;
    private LinearLayout item1_layout;
    private LinearLayout item2_layout;
    private LinearLayout item3_layout;
    private LinearLayout item4_layout;
    private LinearLayout item5_layout;

    Context context;
    private TextView item_text1;
    private TextView item_text2;
    private TextView item_text3;
    private TextView item_text4;
    private TextView item_text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        mViewPager = findViewById(R.id.banner_view);
        item1_layout = findViewById(R.id.item1_layout);
        item2_layout = findViewById(R.id.item2_layout);
        item3_layout = findViewById(R.id.item3_layout);
        item4_layout = findViewById(R.id.item4_layout);
        item5_layout = findViewById(R.id.item5_layout);
        item_text1 = findViewById(R.id.item_text1);
        item_text2 = findViewById(R.id.item_text2);
        item_text3 = findViewById(R.id.item_text3);
        item_text4 = findViewById(R.id.item_text4);
        item_text5 = findViewById(R.id.item_text5);
        //初始化Banner
        initBanner();
        initClick();
    }

    private void initClick() {
        item1_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailVideoActivity.class);
                //CharSequence text = item_text1.getText();
                intent.putExtra("video", R.raw.car);
                intent.putExtra("img1", R.mipmap.a1);
                intent.putExtra("img2", R.mipmap.a2);
                intent.putExtra("str1", R.string.str1);
                intent.putExtra("str2", R.string.str2);
                startActivity(intent);
            }
        });
        item2_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detail1Activity.class);
                CharSequence text = item_text2.getText();
                intent.putExtra("title_text", text);
                intent.putExtra("img1", R.mipmap.a3);
                intent.putExtra("img2", R.mipmap.a6);
                intent.putExtra("str1", R.string.str3);
                intent.putExtra("str2", R.string.str7);
                startActivity(intent);
            }
        });
        item3_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detail1Activity.class);
                CharSequence text = item_text3.getText();
                intent.putExtra("title_text", text);
                intent.putExtra("img1", R.mipmap.a6);
                intent.putExtra("img2", R.mipmap.a7);
                intent.putExtra("str1", R.string.str5);
                intent.putExtra("str2", R.string.str6);
                startActivity(intent);
            }
        });
        item4_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detail1Activity.class);
                CharSequence text = item_text4.getText();
                intent.putExtra("title_text", text);
                intent.putExtra("img1", R.mipmap.a9);
                intent.putExtra("img2", R.mipmap.a10);
                intent.putExtra("str1", R.string.str7);
                intent.putExtra("str2", R.string.str4);
                startActivity(intent);
            }
        });
        item5_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Detail1Activity.class);
                CharSequence text = item_text5.getText();
                intent.putExtra("title_text", text);
                intent.putExtra("img1", R.mipmap.a7);
                intent.putExtra("img2", R.mipmap.a1);
                intent.putExtra("str1", R.string.str5);
                intent.putExtra("str2", R.string.str9);
                startActivity(intent);
            }
        });
    }

    @SuppressLint("NewApi")
    private void initBanner() {
        //构建数据
        imagList = new ArrayList<>();
        imagList.add(getResources().getIdentifier("a2", "mipmap", this.getPackageName()));
        imagList.add(getResources().getIdentifier("a3", "mipmap", this.getPackageName()));
        imagList.add(getResources().getIdentifier("a4", "mipmap", this.getPackageName()));
        imagList.add(getResources().getIdentifier("a5", "mipmap", this.getPackageName()));
        imagList.add(getResources().getIdentifier("a6", "mipmap", this.getPackageName()));

        mViewPager
                .setAutoPlay(true)
                .setScrollDuration(800)
                .setIndicatorStyle(IndicatorStyle.ROUND_RECT)
                .setIndicatorSliderGap(getResources().getDimensionPixelOffset(R.dimen.dp_4))
                .setIndicatorSliderWidth(getResources().getDimensionPixelOffset(R.dimen.dp_4), getResources().getDimensionPixelOffset(R.dimen.dp_10))
                .setIndicatorSliderColor(getColor(R.color.red_normal_color), getColor(R.color.red_checked_color))
                .setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
                .setInterval(2000)
                .setPageMargin(getResources().getDimensionPixelOffset(R.dimen.dp_10))
                .setRevealWidth(getResources().getDimensionPixelOffset(R.dimen.dp_10))
                .setAdapter(new ImageResourceAdapter(getResources().getDimensionPixelOffset(R.dimen.dp_8)))
                .registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int position) {
                        super.onPageSelected(position);
                        //这是指示轮播到那个item
                        Log.i("1111", "========" + position + "====");
                    }
                })
                .setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
                    @Override
                    public void onPageClick(int position) {
                        switch (position) {
                            case 0:
                           /*     Intent intent = new Intent(context, Detail1Activity.class);
                                intent.putExtra("title_text", "英雄上分攻略,教你成为高手");
                                intent.putExtra("img1", R.mipmap.a9);
                                intent.putExtra("img2", R.mipmap.a12);
                                intent.putExtra("str1", R.string.str11);
                                intent.putExtra("str2", R.string.str10);
                                startActivity(intent);*/

                                Intent intent = new Intent(context, DetailVideoActivity.class);
                                //CharSequence text = item_text1.getText();
                                intent.putExtra("video", R.raw.test);
                                intent.putExtra("img1", R.mipmap.a8);
                                intent.putExtra("img2", R.mipmap.a6);
                                intent.putExtra("str1", R.string.str4);
                                intent.putExtra("str2", R.string.str11);
                                startActivity(intent);
                                break;
                            case 1:
                                Intent intent1 = new Intent(context, Detail1Activity.class);
                                intent1.putExtra("title_text", "激情夏日，成为你的英雄");
                                intent1.putExtra("img1", R.mipmap.a3);
                                intent1.putExtra("img2", R.mipmap.a4);
                                intent1.putExtra("str1", R.string.str10);
                                intent1.putExtra("str2", R.string.str5);
                                startActivity(intent1);
                                break;
                            case 2:
                                Intent intent2 = new Intent(context, Detail1Activity.class);
                                intent2.putExtra("title_text", "兄弟一起来，成就峡谷梦");
                                intent2.putExtra("img1", R.mipmap.a7);
                                intent2.putExtra("img2", R.mipmap.a6);
                                intent2.putExtra("str1", R.string.str7);
                                intent2.putExtra("str2", R.string.str6);
                                startActivity(intent2);
                                break;
                            case 3:
                                Intent intent3 = new Intent(context, Detail1Activity.class);
                                intent3.putExtra("title_text", "干就完了，不要多说话");
                                intent3.putExtra("img1", R.mipmap.a5);
                                intent3.putExtra("img2", R.mipmap.a4);
                                intent3.putExtra("str1", R.string.str1);
                                intent3.putExtra("str2", R.string.str3);
                                startActivity(intent3);
                                break;
                            case 4:
                                Intent intent4 = new Intent(context, Detail1Activity.class);
                                intent4.putExtra("title_text", "快上车，等你一起来");
                                intent4.putExtra("img1", R.mipmap.a2);
                                intent4.putExtra("img2", R.mipmap.a10);
                                intent4.putExtra("str1", R.string.str8);
                                intent4.putExtra("str2", R.string.str7);
                                startActivity(intent4);
                                break;
                        }
                        //这是指示点击那个item

                    }
                })
                .create(imagList);
        mViewPager.removeDefaultPageTransformer();

    }

    @Override
    public void onPause() {
        super.onPause();
        if (mViewPager != null) {
            mViewPager.stopLoop();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mViewPager != null)
            mViewPager.startLoop();
    }
}
